package ru.ipccenter.jpa;

import java.util.List;
import java.util.Vector;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

/**
 *
 * @author Krems
 */
@ManagedBean
@SessionScoped
public class LoginBean {
    
    static EntityManager em;
    public LoginBean() {
        em = Persistence.createEntityManagerFactory("RegisterPU").createEntityManager();
    }

    private String login;
    private String password;
    private String buttonValue = "Login";
    private boolean register = false;
    private String errorText = "";

    public String getErrorText() {
        return errorText;
    }

    public void setButtonValue(String buttonValue) {
        this.buttonValue = buttonValue;
    }

    public void setErrorText(String errorText) {
        this.errorText = errorText;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }

    public String onButtonClick() {
        if (register) {
            if (JPAControllerClass.register(login, password)) {
                return "/personalPage";
            } else {
                this.errorText = "Try the other login";
                return "";
            }
        } else {
            if (JPAControllerClass.login(login, password)) {
                return "/personalPage";
            } else {
                this.errorText = "Wrong login/password";
            }
                return "";
        }
    }

    public void setRegister(boolean register) {
        this.register = register;
    }

    public boolean isRegister() {
        return register;
    }

    public String getButtonValue() {
        return buttonValue;
    }
    
    public void change() {
        if (!register) {
            this.buttonValue = "Register";
        } else {
            this.buttonValue = "Login";
        }
    }
    
    private String server = "dgap-gw.campus";
    public List<String> getAllPaths() {
        return em.createNamedQuery("findAllPaths").setParameter("table", server).getResultList();
    }
}


class JPAControllerClass {    
    public static boolean login(String login, String password) {
        Vector<String> pass =
            (Vector<String>) 
                LoginBean.em.createNamedQuery("login").setParameter("login",
                login).getResultList();
        if (pass.isEmpty()) {
            return false;
        }
        if (password.equals(pass.get(0)))
            return true;
        else
            return false;
    }
    
    public static boolean register(String login, String password) {
        Vector<Long> id =
                (Vector<Long>)
                    LoginBean.em.createNamedQuery("loginExist").setParameter("login",
                login).getResultList();
        if (!id.isEmpty()) {
            return false;
        } else {
            EntityTransaction transaction = LoginBean.em.getTransaction();
            transaction.begin();
            LoginEntity user = new LoginEntity(login, password);
            LoginBean.em.persist(user);
            transaction.commit();
            return true;
        }
    }
}